package top.psjj.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.psjj.mall.comment.Constant;
import top.psjj.mall.exception.MallException;
import top.psjj.mall.exception.MallExceptionEnum;
import top.psjj.mall.mapper.CategoryMapper;
import top.psjj.mall.mapper.ProductMapper;
import top.psjj.mall.model.query.ProductListQuery;
import top.psjj.mall.model.dto.ProductListReq;
import top.psjj.mall.model.po.Product;
import top.psjj.mall.service.api.ProductService;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void save(Product product) {
        //先查询商品名字是否存在
        Product oldProduct = productMapper.selectByName(product.getName());
        if(oldProduct!=null){
            throw new MallException(MallExceptionEnum.EXISTS_NAME);
        }
        //插入数据
        productMapper.insertSelective(product);
    }

    @Override
    public void update(Product product) {
        //先判断名字是否存在
        Product oldProduct = productMapper.selectByName(product.getName());
        if(oldProduct!=null){
            if(product.getName()!=null&&product.getId()!=oldProduct.getId()){
                throw new MallException(MallExceptionEnum.EXISTS_NAME);
            }
        }
        //修改数据
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public void delete(Integer id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateBatchSellStatus(Integer[] ids, Integer sellStatus) {
        productMapper.updateBatchSellStatus(ids,sellStatus);
    }

    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize,"update_time desc");
        List<Product> products = productMapper.selectByExample(null);
        PageInfo pageInfo = new PageInfo(products);
        return pageInfo;
    }

    @Override
    public Product detail(Integer id) {
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    }

    @Override
    public PageInfo listForUser(ProductListReq productListReq) {
        //构建Query对象
        ProductListQuery productListQuery = new ProductListQuery();
        //搜索处理
        if(StringUtils.hasText(productListReq.getKeyword())){
            String keyword = new StringBuffer().append("%").append(productListReq.getKeyword()).append("%").toString();
            productListQuery.setKeyword(keyword);
        }
        //分类id处理
        if(productListReq.getCategoryId()!=null){
            List<Integer> categoryIds = getCategoryIds(productListReq.getCategoryId());
            productListQuery.setCategoryIds(categoryIds);
        }
        //排序处理
        String orderBy = productListReq.getOrderBy();
        if(Constant.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
            PageHelper.startPage(productListReq.getPageNum(),productListReq.getPageSize(),orderBy);
        }else {
            PageHelper.startPage(productListReq.getPageNum(),productListReq.getPageSize());
        }
        List<Product> list = productMapper.selectByList(productListQuery);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
    private List<Integer> getCategoryIds(Integer categoryId){
        List<Integer> categoryIds = new ArrayList<Integer>();
        categoryIds.add(categoryId);
        List<Integer> list = categoryMapper.selectIdByParentId(categoryId);
        if(list!=null && list.size()>0){
            for(Integer id : list){
                categoryIds.add(id);
            }
        }
        return categoryIds;
    }

}
